<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    class EventEmitter {
        map;

        constructor() {
            this.map = new Map();
        }


        /**
         * @param {string} eventName
         * @param {Function} callback
         * @return {Object}
         */
        subscribe(eventName, callback) {
            this.map.has(eventName) ? this.map.get(eventName).add(callback) : this.map.set(eventName, new Set(callback))
            return {
                unsubscribe: () => {
                    this.map.get(eventName).delete(callback)
                    return;
                }
            };
        }

        /**
         * @param {string} eventName
         * @param {Array} args
         * @return {Array}
         */
        emit(eventName, args = []) {
            if(this.map.has(eventName)){
                let func = this.map.has(eventName)
                let res = [];
                for (let i = 0; i < func.length; i++) {
                    res[i] = func[i](...args)
                }
                return res;
            }else{
                return [];
            }
        }
    }

    /**
     * const emitter = new EventEmitter();
     *
     * // Subscribe to the onClick event with onClickCallback
     * function onClickCallback() { return 99 }
     * const sub = emitter.subscribe('onClick', onClickCallback);
     *
     * emitter.emit('onClick'); // [99]
     * sub.unsubscribe(); // undefined
     * emitter.emit('onClick'); // []
     */
    var res;
    const emitter = new EventEmitter();
    res = emitter.emit("firstEvent"); // [], 还没有订阅任何回调函数
    console.log(res)
    res = emitter.subscribe("firstEvent", function cb1() {
        return 5;
    });
    res = emitter.subscribe("firstEvent", function cb2() {
        return 6;
    });
    res = emitter.emit("firstEvent"); // [5, 6], 返回 cb1 和 cb2 的输出
    console.log(res)

</script>
</html>